﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Model;
using VAdvantage.Utility;
using VAdvantage.Classes;
using VAdvantage.Grids;
using VAdvantage.Controls;
using VAdvantage.Logging;
using System.Collections;
using System.Text;
using System.Data;
using VAdvantage.DataBase;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Data;
using VAdvantage.Process;

namespace VAdvantage.Apps.AForms
{
    public partial class VCharge : UserControl, FormPanel
    {
        #region Private Variables

        VAdvantage.Grids.MyDataGridCheckBoxColumn chk = new MyDataGridCheckBoxColumn();
        /**	Window No			*/
        private int _windowNo = 0;
        /**	FormFrame			*/
        private FormFrame _frame;

        /** Account Element     */
        private int m_C_Element_ID = 0;
        /** AccountSchema       */
        private int m_C_AcctSchema_ID = 0;
        /** Default Charge Tax Category */
        private int m_C_TaxCategory_ID = 0;
        private int m_AD_Client_ID = 0;
        private int m_AD_Org_ID = 0;
        /**	Logger			*/

        //Logger			
        private static VLogger log = VLogger.GetVLogger(typeof(VCharge).FullName);

        #endregion

        /// <summary>
        /// 
        /// </summary>
        public VCharge()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initialize Panel
        /// </summary>
        /// <param name="WindowNo"> Window No</param>
        /// <param name="frame"> Frame</param>
        public void Init(int WindowNo, FormFrame frame)
        {
            SetBusy(true);
            log.Info("");
            _windowNo = WindowNo;
            _frame = frame;
            try
            {
                DynInit();
                GridColumns();
                chk.Click += new RoutedEventHandler(chk_Click);
                this.Visibility = System.Windows.Visibility.Visible;
                _frame.ContentGrid.Children.Add(this);
                _frame.Focus();
                //_frame.Height = 700;
                //_frame.Width = 500;
                _frame.SetTitle(Msg.GetMsg(Envs.GetCtx(), "GenerateCharges"));
                vchkExpense.IsChecked = true;
                Culturing();
                SetBusy(false);
                vchkSelectAll.Visibility = Visibility.Collapsed;
            }
            catch (Exception e)
            {
                log.Log(Level.SEVERE, "", e);
                SetBusy(false);
            }

        }

        public void Load()
        {

            SetBusy(true);
            try
            {
                DynInit();
                GridColumns();
                chk.Click += new RoutedEventHandler(chk_Click);
                this.Visibility = System.Windows.Visibility.Visible;
                //_frame.ContentGrid.Children.Add(this);
                //_frame.Focus();
                ////_frame.Height = 700;
                ////_frame.Width = 500;
                //_frame.SetTitle(Msg.GetMsg(Envs.GetCtx(), "GenerateCharges"));
                vchkExpense.IsChecked = true;
                Culturing();
                vbtnOK.Visibility = Visibility.Collapsed;
                groupBox1.Visibility = Visibility.Collapsed;
                vchkSelectAll.Visibility = Visibility.Visible;
                SetBusy(false);
            }
            catch (Exception e)
            {
                log.Log(Level.SEVERE, "", e);
                SetBusy(false);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void chk_Click(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// Loading Defaults at startup.
        /// </summary>
        private void DynInit()
        {
            m_C_AcctSchema_ID = Envs.GetCtx().GetContextAsInt("$C_AcctSchema_ID");
            //  get Element

            //  Other Defaults
            m_AD_Client_ID = Envs.GetCtx().GetAD_Client_ID();
            m_AD_Org_ID = Envs.GetCtx().GetAD_Org_ID();
            Envs.GetCtx().GetAD_User_ID();

            System.Threading.ThreadPool.QueueUserWorkItem(delegate
            {
                m_C_Element_ID = GetC_Element_ID(m_C_AcctSchema_ID);
                if (m_C_Element_ID == 0)
                    return;
                m_C_TaxCategory_ID = GetC_Tax_Category_ID(m_AD_Client_ID);
                DataSet ds = FillGrid(m_C_Element_ID);
                Dispatcher.BeginInvoke(delegate
                {
                    if (ds != null)
                    {
                        BindingSource source = new BindingSource(ds.Tables[0].GetBindableData(new Connector()));
                        vdgvCharge.ItemsSource = source;
                    }
                });
            });
        }


        /// <summary>
        /// Dispose
        /// </summary>
        public void Dispose()
        {
            if (_frame != null)
            {
                _frame.Dispose();
            }
            _frame = null;
        }

        /// <summary>
        /// Create Charge Button Click will create a new charge based on the values in the name and search key fields.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void vbtnCreateCharge_Click(object sender, RoutedEventArgs e)
        {
            SetBusy(true);
            bool expense = Util.GetValueOfBool(vchkExpense.IsChecked);
            log.Config("");
            //  Get Input
            String value = vtxtSearchKey.Text;
            if (value.Length == 0)
            {
                ShowMessage.Info("SearchKeyMandatory", true, null, null);
                // valueField.setBackground(ViennaPLAF.getFieldBackground_Error());
                SetBusy(false);
                return;
            }
            String name = vtxtName.Text;
            if (name.Length == 0)
            {
                ShowMessage.Info("NameMandatory", true, null, null);
                // nameField.setBackground(ViennaPLAF.getFieldBackground_Error());
                SetBusy(false);
                return;
            }

            System.Threading.ThreadPool.QueueUserWorkItem(delegate
            {

                //  Create Element
                int C_ElementValue_ID = Create_ElementValue(Envs.GetCtx(), m_AD_Org_ID, value, name, expense, m_C_Element_ID);
                if (C_ElementValue_ID == 0)
                {
                    log.SaveError("ChargeNotCreated", "ChargeNotCreated");
                    //ADialog.error(m_WindowNo, this, "ChargeNotCreated", name);
                    SetBusy(false);
                    return;
                }
                //  Create Charge
                int C_Charge_ID = Create_Charge(Envs.GetCtx(), m_C_AcctSchema_ID, m_C_TaxCategory_ID, name, C_ElementValue_ID, expense);
                if (C_Charge_ID == 0)
                {
                    log.SaveError("ChargeNotCreated", "ChargeNotCreated");
                    //ADialog.error(m_WindowNo, this, "ChargeNotCreated", name);
                    SetBusy(false);
                    return;
                }

                Dispatcher.BeginInvoke(delegate
                {
                    //ADialog.info(m_WindowNo, this, "ChargeCreated", name);
                    ShowMessage.Info("ChargeCreated", true, null, null);
                    SetBusy(false);
                });

            });

        }

       /// <summary>
       /// Gets the C_Element_ID
       /// </summary>
       /// <param name="m_C_AcctSchema_ID"></param>
       /// <returns>C_Element_ID</returns>
        public static int GetC_Element_ID(int m_C_AcctSchema_ID)
        {
            int m_C_Element_ID = 0;
            String sql = "SELECT C_Element_ID FROM C_AcctSchema_Element "
                + "WHERE ElementType='AC' AND C_AcctSchema_ID= " + m_C_AcctSchema_ID;
            try
            {
                m_C_Element_ID = Util.GetValueOfInt(DB.ExecuteScalar(sql, null, null));
            }
            catch (Exception e)
            {
                log.Log(Level.SEVERE, sql, e);
            }
            return m_C_Element_ID;
        }

        /// <summary>
        /// Get Tax Category ID which is default based on the Client ID
        /// </summary>
        /// <param name="m_AD_Client_ID"></param>
        /// <returns></returns>
        public static int GetC_Tax_Category_ID(int m_AD_Client_ID)
        {
            //  TaxCategory
            String sql = "SELECT C_TaxCategory_ID FROM C_TaxCategory "
                + "WHERE IsDefault='Y' AND AD_Client_ID=" + m_AD_Client_ID;
            int m_C_TaxCategory_ID = 0;
            try
            {
                m_C_TaxCategory_ID = Util.GetValueOfInt(DB.ExecuteScalar(sql, null, null));
            }
            catch (Exception e)
            {
                log.Log(Level.SEVERE, sql, e);
            }
            return m_C_TaxCategory_ID;
        }

        /// <summary>
        /// Grid Columns are generated here
        /// </summary>
        private void GridColumns()
        {
            chk.Header = Msg.GetMsg(Envs.GetCtx(), "SELECT");
            chk.Binding = new System.Windows.Data.Binding("SELECT1");
            chk.DataPropertyName = "SELECT1";
            chk.DataGrid = vdgvCharge;
            chk.IsThreeState = false;
            chk.IsReadOnly = false;
            vdgvCharge.Columns.Add(chk);

            DataGridTextColumn eValue = new DataGridTextColumn();
            eValue.Header = Msg.GetMsg(Envs.GetContext(), "C_ELEMENTVALUE_ID");
            eValue.Binding = new System.Windows.Data.Binding("C_ELEMENTVALUE_ID");
            eValue.IsReadOnly = true;
            eValue.Visibility = Visibility.Collapsed;
            vdgvCharge.Columns.Add(eValue);

            DataGridTextColumn sKey = new DataGridTextColumn();
            sKey.Header = Msg.GetMsg(Envs.GetContext(), "SearchKey");
            sKey.Binding = new System.Windows.Data.Binding("VALUE");
            sKey.IsReadOnly = true;
            sKey.Visibility = Visibility.Visible;
            vdgvCharge.Columns.Add(sKey);

            DataGridTextColumn Name = new DataGridTextColumn();
            Name.Header = Msg.GetMsg(Envs.GetContext(), "Name");
            Name.Binding = new System.Windows.Data.Binding("NAME");
            Name.IsReadOnly = true;
            Name.Visibility = Visibility.Visible;
            vdgvCharge.Columns.Add(Name);

            DataGridCheckBoxColumn exp = new DataGridCheckBoxColumn();
            exp.Header = Msg.GetMsg(Envs.GetCtx(), "Expense");
            exp.Binding = new System.Windows.Data.Binding("EXPENSE");
            exp.IsThreeState = false;
            exp.IsReadOnly = false;
            exp.Visibility = Visibility.Visible;
            vdgvCharge.Columns.Add(exp);

        }

        /// <summary>
        /// Sets the Culturing for the Controls
        /// </summary>
        private void Culturing()
        {
            groupBox1.Header = Msg.GetMsg(Envs.GetCtx(), "CreateAccountandCharge");
            groupBox2.Header = Msg.GetMsg(Envs.GetCtx(), "CreateChargefromAccount");
            //  grpBox1.Text = Msg.GetMsg(Envs.GetCtx(), "CreateAccountandCharge");
            //  grpBox2.Text = Msg.GetMsg(Envs.GetCtx(), "CreateChargefromAccount");
            vchkExpense.Content = Msg.GetMsg(Envs.GetCtx(), "Expense");
            vbtnCreateCharge.Content = Msg.GetMsg(Envs.GetCtx(), "Create");
            vlblSearchKey.Content = Msg.GetMsg(Envs.GetCtx(), "SearchKey");
            vlblName.Content = Msg.GetMsg(Envs.GetCtx(), "Name");
            vchkSelectAll.Content = Msg.GetMsg(Envs.GetCtx(), "SelectAll");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="m_AD_Org_ID"></param>
        /// <param name="value"></param>
        /// <param name="name"></param>
        /// <param name="isExpenseType"></param>
        /// <param name="m_C_Element_ID"></param>
        /// <returns></returns>
        public static int Create_ElementValue(Ctx ctx, int m_AD_Org_ID, String value, String name, Boolean isExpenseType, int m_C_Element_ID)
        {
            log.Config(name);
            //
            MElementValue ev = new MElementValue(ctx, value, name, null,
                isExpenseType ? X_C_ElementValue.ACCOUNTTYPE_Expense : X_C_ElementValue.ACCOUNTTYPE_Revenue,
                    X_C_ElementValue.ACCOUNTSIGN_Natural,
                    false, false, null);
            ev.SetC_Element_ID(m_C_Element_ID);
            ev.SetAD_Org_ID(m_AD_Org_ID);
            if (!ev.Save())
            {
                log.Log(Level.WARNING, "C_ElementValue_ID not created");
            }
            return ev.GetC_ElementValue_ID();
        }   //  create_ElementValue

        /// <summary>
        /// Create new Charge based on the parameters passed
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="m_C_AcctSchema_ID"></param>
        /// <param name="m_C_TaxCategory_ID"></param>
        /// <param name="name"></param>
        /// <param name="primaryC_ElementValue_ID"></param>
        /// <param name="expense"></param>
        /// <returns></returns>
        public static int Create_Charge(Ctx ctx, int m_C_AcctSchema_ID, int m_C_TaxCategory_ID, String name, int primaryC_ElementValue_ID, Boolean expense)
        {
            log.Config(name + " - ");
            //
            MCharge charge = new MCharge(ctx, 0, null);
            charge.SetName(name);
            charge.SetC_TaxCategory_ID(m_C_TaxCategory_ID);
            if (!charge.Save())
            {
                log.Log(Level.SEVERE, name + " not created");
                return 0;
            }

            MAcctSchema m_acctSchema = null;
            //  Get Primary AcctSchama
            if (m_acctSchema == null)
                m_acctSchema = new MAcctSchema(ctx, m_C_AcctSchema_ID, null);
            if (m_acctSchema == null || m_acctSchema.GetC_AcctSchema_ID() == 0)
                return 0;
            MAcctSchemaElement primary_ase = m_acctSchema.GetAcctSchemaElement(X_C_AcctSchema_Element.ELEMENTTYPE_Account);

            //	Get All
            MAcctSchema[] ass = MAcctSchema.GetClientAcctSchema(ctx, charge.GetAD_Client_ID());
            foreach (MAcctSchema ac in ass)
            {
                //	Target Account
                MAccount defaultAcct = MAccount.GetDefault(ac, true);	//	optional null
                //	Natural Account
                int C_ElementValue_ID = primaryC_ElementValue_ID;
                MAcctSchemaElement ase = ac.GetAcctSchemaElement(X_C_AcctSchema_Element.ELEMENTTYPE_Account);
                if (primary_ase.GetC_Element_ID() != ase.GetC_Element_ID())
                {
                    MAcctSchemaDefault defAccts = MAcctSchemaDefault.Get(ctx, ac.GetC_AcctSchema_ID());
                    int C_ValidCombination_ID = defAccts.GetCh_Expense_Acct();
                    if (!expense)
                    {
                        C_ValidCombination_ID = defAccts.GetCh_Revenue_Acct();
                    }
                    MAccount chargeAcct = MAccount.Get(ctx, C_ValidCombination_ID);
                    C_ElementValue_ID = chargeAcct.GetAccount_ID();
                    //	Fallback
                    if (C_ElementValue_ID == 0)
                    {
                        C_ElementValue_ID = defaultAcct.GetAccount_ID();
                        if (C_ElementValue_ID == 0)
                        {
                            C_ElementValue_ID = ase.GetC_ElementValue_ID();
                        }
                        if (C_ElementValue_ID == 0)
                        {
                            log.Log(Level.WARNING, "No Default ElementValue for " + ac);
                            continue;
                        }
                    }
                }

                MAccount acct = MAccount.Get(ctx,
                    charge.GetAD_Client_ID(), charge.GetAD_Org_ID(),
                    ac.GetC_AcctSchema_ID(),
                    C_ElementValue_ID, defaultAcct.GetC_SubAcct_ID(),
                    defaultAcct.GetM_Product_ID(), defaultAcct.GetC_BPartner_ID(), defaultAcct.GetAD_OrgTrx_ID(),
                    defaultAcct.GetC_LocFrom_ID(), defaultAcct.GetC_LocTo_ID(), defaultAcct.GetC_SalesRegion_ID(),
                    defaultAcct.GetC_Project_ID(), defaultAcct.GetC_Campaign_ID(), defaultAcct.GetC_Activity_ID(),
                    defaultAcct.GetUser1_ID(), defaultAcct.GetUser2_ID(),
                    defaultAcct.GetUserElement1_ID(), defaultAcct.GetUserElement2_ID());
                if (acct == null)
                {
                    log.Log(Level.WARNING, "No Default Account for " + ac);
                    continue;
                }

                //  Update Accounts
                StringBuilder sql = new StringBuilder("UPDATE C_Charge_Acct ");
                sql.Append("SET CH_Expense_Acct=").Append(acct.GetC_ValidCombination_ID());
                sql.Append(", CH_Revenue_Acct=").Append(acct.GetC_ValidCombination_ID());
                sql.Append(" WHERE C_Charge_ID=").Append(charge.GetC_Charge_ID());
                sql.Append(" AND C_AcctSchema_ID=").Append(ac.GetC_AcctSchema_ID());
                //
                int no = DB.ExecuteQuery(sql.ToString(), null, null);
                if (no != 1)
                {
                    log.Log(Level.WARNING, "Update #" + no + "\n" + sql.ToString());
                }
            }
            //
            return charge.GetC_Charge_ID();
        }

        /// <summary>
        /// Fills the Grid when the form is loaded.
        /// </summary>
        /// <param name="m_C_Element_ID"></param>
        /// <returns></returns>
        private DataSet FillGrid(int m_C_Element_ID)
        {
            String sql = "SELECT 'false' as Select1, C_ElementValue_ID,Value, Name, case when AccountType = 'E' THEN 'true' else 'false' end as Expense"
             + " FROM C_ElementValue "
             + " WHERE AccountType IN ('R','E')"
             + " AND IsSummary='N'"
             + " AND C_Element_ID= " + m_C_Element_ID
             + " ORDER BY 2";
            DataSet ds = null;
            try
            {
                ds = DB.ExecuteDataset(sql, null, null);
            }
            catch (Exception e)
            {
                log.Log(Level.SEVERE, sql, e);
            }
            return ds;
        }

        /// <summary>
        /// Disposes the form when OK button is pressed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void vbtnOK_Click(object sender, RoutedEventArgs e)
        {
            Dispose();
        }

        /// <summary>
        /// Generates Charge based on the Rows selected in the Grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void vbtnAccount_Click(object sender, RoutedEventArgs e)
        {
            SetBusy(true);
            List<int> eleValue_ID = new List<int>();
            List<string> eleName = new List<string>();
            List<bool> expense = new List<bool>();
            log.Config("");
            ////
            StringBuilder listCreated = new StringBuilder();
            StringBuilder listRejected = new StringBuilder();

            BindingSource rowSource = vdgvCharge.ItemsSource as BindingSource;
            if (rowSource.Count != 0)
            {
                for (int i = 0; i < rowSource.Count; i++)
                {
                    Boolean isSelected = Convert.ToBoolean(((BindableObject)rowSource[i]).GetValue("SELECT1"));
                    if (isSelected)
                    {
                        int C_ElementValue_ID = Util.GetValueOfInt(((BindableObject)rowSource[i]).GetValue("C_ELEMENTVALUE_ID"));
                        String name = Util.GetValueOfString(((BindableObject)rowSource[i]).GetValue("NAME"));
                        Boolean exp = Convert.ToBoolean(((BindableObject)rowSource[i]).GetValue("EXPENSE"));

                        eleValue_ID.Add(C_ElementValue_ID);
                        eleName.Add(name);
                        expense.Add(exp);
                       
                        ((BindableObject)rowSource[i]).SetValue("SELECT1", "false");
                       
                        //  vdgvCharge.Columns[0].IsReadOnly = true;
                        //  reset selection
                        // vdgvCharge.Rows[i].Cells[0].Value = false;
                    }
                }

                System.Threading.ThreadPool.QueueUserWorkItem(delegate
                {

                    for (int j = 0; j < eleValue_ID.Count; j++)
                    {
                        int C_Charge_ID = Create_Charge(Envs.GetCtx(), m_C_AcctSchema_ID, m_C_TaxCategory_ID, eleName[j], eleValue_ID[j], expense[j]);
                        if (C_Charge_ID == 0)
                        {
                            if (listRejected.Length > 0)
                            {
                                listRejected.Append(", ");
                            }
                            listRejected.Append(eleName[j]);
                        }
                        else
                        {
                            if (listCreated.Length > 0)
                            {
                                listCreated.Append(", ");
                            }
                            listCreated.Append(eleName[j]);
                        }
                    }
                    Dispatcher.BeginInvoke(delegate
                    {
                        if (listCreated.Length > 0)
                        {
                            ShowMessage.Info("ChargeCreated", true, null, null);
                        }
                        if (listRejected.Length > 0)
                        {
                            ShowMessage.Info("ChargeNotCreated", true, null, null);
                        }
                        SetBusy(false);
                    });
                });
            }

            ////
            //// TableModel model = dataTable.getModel();
            //int rows = vdgvCharge.Rows.Count;
            //for (int i = 0; i < rows; i++)
            //{
            //    //if (((Boolean)model.getValueAt(i, 0)).booleanValue())
            //    if (Util.GetValueOfString(vdgvCharge.Rows[i].Cells[0].Value).ToUpper() == "TRUE")
            //    {
            //        // KeyNamePair pp = (KeyNamePair)model.getValueAt(i, 1);
            //        int C_ElementValue_ID = Util.GetValueOfInt(vdgvCharge.Rows[i].Cells[1].Value);
            //        String name = Util.GetValueOfString(vdgvCharge.Rows[i].Cells[3].Value);
            //        String exp = Util.GetValueOfString(vdgvCharge.Rows[i].Cells[4].Value);
            //        Boolean expense = false;
            //        if (exp.ToUpper() == "TRUE")
            //        {
            //            expense = true;
            //        }

            //        //
            //        int C_Charge_ID = Create_Charge(Envs.GetCtx(), m_C_AcctSchema_ID, m_C_TaxCategory_ID, name, C_ElementValue_ID, expense);
            //        if (C_Charge_ID == 0)
            //        {
            //            if (listRejected.Length > 0)
            //            {
            //                listRejected.Append(", ");
            //            }
            //            listRejected.Append(name);
            //        }
            //        else
            //        {
            //            if (listCreated.Length > 0)
            //            {
            //                listCreated.Append(", ");
            //            }
            //            listCreated.Append(name);
            //        }
            //        //  reset selection
            //        //model.setValueAt(Boolean.valueOf(false), i, 0);
            //        // vdgvCharge.Rows[i].Cells[0] = false;
            //        vdgvCharge.Rows[i].Cells[0].Value = false;
            //    }
            //}
            //if (listCreated.Length > 0)
            //{
            //    ShowMessage.Info("ChargeCreated", true, null, null);
            //    // ADialog.info(m_WindowNo, this, "ChargeCreated", listCreated.toString());
            //}
            //if (listRejected.Length > 0)
            //{
            //    ShowMessage.Info("ChargeNotCreated", true, null, null);
            //    // ADialog.error(m_windowNo, this, "ChargeNotCreated", listRejected.toString());
            //}

        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="value"></param>
        private void SetBusy(bool value)
        {
            this.IsEnabled = !value;
            busyWindow.Visibility = value ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            // txtBusy.Visibility = value ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void busyWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void vchkSelectAll_Checked(object sender, RoutedEventArgs e)
        {
             BindingSource rowSource = vdgvCharge.ItemsSource as BindingSource;
             if (rowSource.Count != 0)
             {
                 for (int i = 0; i < rowSource.Count; i++)
                 {

                     ((BindableObject)rowSource[i]).SetValue("SELECT1", "true");

                 }
             }

           
        }

        private void vchkSelectAll_Unchecked(object sender, RoutedEventArgs e)
        {
            BindingSource rowSource = vdgvCharge.ItemsSource as BindingSource;
            if (rowSource.Count != 0)
            {
                for (int i = 0; i < rowSource.Count; i++)
                {

                    ((BindableObject)rowSource[i]).SetValue("SELECT1", "false");

                }
            }
        }
    }
}
